﻿Imports System.Data.OleDb
Public Class frmProveedor
    Dim coneccion As OleDbConnection
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        actulizaGrid()
        DataGridView2.RowHeadersVisible = False
        DataGridView2.ClearSelection()

        cmbCriterioBusqueda.Items.Add("Id")
        cmbCriterioBusqueda.Items.Add("Nombre")

        cmbCriterioBusqueda.SelectedItem = "Id"

        DataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill

    End Sub
    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        If btnGuardar.Text = "Nuevo" Then
            limpiaCampos()
            btnEliminar.Enabled = False
            btnModificar.Enabled = False
            btnGuardar.Text = "Guardar"

            txtNombre.Focus()
        Else
            Dim command As OleDbCommand

            Dim query1 As String = "insert into Proveedor(Nombre,Cedula_Juridica,Empresa,Direccion,Telefono) values('" & txtNombre.Text & "'," & txtCedula.Text & ",'" & txtEmpresa.Text & "','" & txtDireccion.Text & "'," & txtTelefono.Text & ")"
            command = New OleDbCommand(query1, coneccion)
            command.Connection.Open()
            command.ExecuteNonQuery()
            command.Connection.Close()

            MsgBox("El registro fue Ingresado")

            actulizaGrid()
            limpiaCampos()
        End If
    End Sub
    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        Dim command As OleDbCommand

        coneccion = New OleDbConnection(bd.sConexion)

        Dim query1 As String = "delete from Proveedor WHERE IdProveedor =" & DataGridView2.Item(0, DataGridView2.CurrentRow.Index).Value
        command = New OleDbCommand(query1, coneccion)
        command.Connection.Open()
        command.ExecuteNonQuery()
        command.Connection.Close()

        MsgBox("El registro fue Eliminado")

        command.Connection.Close()

        actulizaGrid()
        limpiaCampos()

        btnGuardar.Text = "Guardar"
        btnEliminar.Enabled = False
        btnModificar.Enabled = False
    End Sub
    Private Sub btnModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificar.Click
        Dim command As OleDbCommand

        coneccion = New OleDbConnection(bd.sConexion)

        Dim query1 As String = "UPDATE Proveedor SET Nombre='" + txtNombre.Text + "', Cedula_Juridica=" + txtCedula.Text + ", Empresa='" + txtEmpresa.Text + "', Direccion='" + txtDireccion.Text + "', Telefono=" + txtTelefono.Text + " WHERE IdProveedor=" & DataGridView2.Item(0, DataGridView2.CurrentRow.Index).Value

        command = New OleDbCommand(query1, coneccion)
        command.Connection.Open()
        command.ExecuteNonQuery()
        command.Connection.Close()
        MsgBox("El registro fue Modificado")

        command.Connection.Close()
        actulizaGrid()
        limpiaCampos()

        btnGuardar.Text = "Guardar"
        btnEliminar.Enabled = False
        btnModificar.Enabled = False
    End Sub
    Sub actulizaGrid()
        Dim orden As OleDbDataAdapter
        Dim tabla As DataSet

        coneccion = New OleDbConnection(bd.sConexion)

        orden = New OleDbDataAdapter("select * from Proveedor", coneccion)
        tabla = New DataSet

        orden.Fill(tabla, "Proveedor")

        DataGridView2.DataSource = tabla

        DataGridView2.DataMember = "Proveedor"
    End Sub
    Private Sub DataGridView2_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellClick

        If IsDBNull(DataGridView2.Item(1, DataGridView2.CurrentRow.Index).Value) Then
            txtNombre.Text = ""
        Else
            txtNombre.Text = DataGridView2.Item(1, DataGridView2.CurrentRow.Index).Value
        End If

        If IsDBNull(DataGridView2.Item(2, DataGridView2.CurrentRow.Index).Value) Then
            txtCedula.Text = ""
        Else
            txtCedula.Text = DataGridView2.Item(2, DataGridView2.CurrentRow.Index).Value
        End If

        If IsDBNull(DataGridView2.Item(3, DataGridView2.CurrentRow.Index).Value) Then
            txtEmpresa.Text = ""
        Else
            txtEmpresa.Text = DataGridView2.Item(3, DataGridView2.CurrentRow.Index).Value
        End If

        If IsDBNull(DataGridView2.Item(4, DataGridView2.CurrentRow.Index).Value) Then
            txtDireccion.Text = ""
        Else
            txtDireccion.Text = DataGridView2.Item(4, DataGridView2.CurrentRow.Index).Value
        End If

        If IsDBNull(DataGridView2.Item(5, DataGridView2.CurrentRow.Index).Value) Then
            txtTelefono.Text = ""
        Else
            txtTelefono.Text = DataGridView2.Item(5, DataGridView2.CurrentRow.Index).Value
        End If

        btnGuardar.Text = "Nuevo"
        btnModificar.Enabled = True
        btnEliminar.Enabled = True
    End Sub
    Sub limpiaCampos()
        txtCedula.Clear()
        txtDireccion.Clear()
        txtEmpresa.Clear()
        txtNombre.Clear()
        txtTelefono.Clear()
        DataGridView2.ClearSelection()
    End Sub
    Private Sub DataGridView2_CellContentClick_2(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellContentClick
        btnModificar.Enabled = True
        btnEliminar.Enabled = True
    End Sub
    Private Sub txtBuscar_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.TextChanged

        ErrorProvider2.Dispose()

        Dim orden As OleDbDataAdapter
        Dim tabla As DataSet

        coneccion = New OleDbConnection(bd.sConexion)
        orden = New OleDbDataAdapter("select * from Proveedor", coneccion)

        If cmbCriterioBusqueda.SelectedIndex = 0 Then
            If txtBuscar.Text.Trim = String.Empty Then
                orden = New OleDbDataAdapter("select * from Proveedor", coneccion)
            Else
                If Char.IsNumber(txtBuscar.Text) Then
                    orden = New OleDbDataAdapter("select * from Proveedor where idProveedor =" + txtBuscar.Text, coneccion)
                Else
                    txtBuscar.Clear()
                    ErrorProvider2.SetError(txtBuscar, "El valor digitado debe ser numerico")
                End If
            End If
        ElseIf cmbCriterioBusqueda.SelectedIndex = 1 Then
            orden = New OleDbDataAdapter("select * from Proveedor where Nombre like '%" + txtBuscar.Text + "%'", coneccion)
        End If

        tabla = New DataSet
        orden.Fill(tabla, "Proveedor")
        DataGridView2.DataSource = tabla
        DataGridView2.DataMember = "Proveedor"
        DataGridView2.ClearSelection()
    End Sub
End Class
